#!/bin/env lua
if not arg or arg[0] == "convert-sv.lua" then
	dofile("config.lua")
end

--[[
	HOWTO :
	You must install the addon "Talented_GatherData" in wow, then log
	a character from each of the 9 classes.
	After this, you must copy the file :
	<WOW_PATH>\WTF\<ACCOUNT>\SavedVariables\Talented_GatherData.lua
	in this directory, then run lua convert-sv.lua to generate the final
	class database files.
]]

dofile("Talented_GatherData.lua")

local build = Talented_GatherDB.latest
local TOC_HEADER = [[
# Generated Data, do not modify
## Interface: %s
## Title: Talented_Data
## Name: Talented_Data
## Notes: Class Data for The Talent Template Editor
## Author: Jerry
## Version: 1.0
## X-AceForum: 3955
## X-Category: Interface Enhancements
## X-ExpectedBuild: %q
## LoadOnDemand: 1

Talented_Data_init.lua
]]

local TOC_ENTRY = "Talented_Data_%s.lua\n"

local TOC_FOOTER = [[
# DO NOT ADD HAND MADE TRANSLATIONS.
# Contact jerry here : http://www.wowace.com/forums/index.php?topic=3955.0
# if you want to add translation
]]

local TOC_VERSION = "30000"

local function write_talent(o, talent)
	o:write(string.format([[
			{
				info = {
					icon = %q,
					row = %d,
					column = %d,
					ranks = %d,
]], talent.info.icon, talent.info.row, talent.info.column, talent.info.ranks))
	if talent.info.exceptional then
		o:write("\t\t\t\t\texceptional = 1,\n")
	end

	local prereqs = talent.info.prereqs
	if prereqs and next(prereqs) then
		o:write("\t\t\t\t\tprereqs = {\n")
		for _, r in ipairs(prereqs) do
			o:write(string.format([[
						{
							row = %d,
							column = %d,
						},
]], r.row, r.column))
		end
		o:write("\t\t\t\t\t},\n")
	end

	o:write([[
				},
			},
]])

end

local function write_tree(o, tree)
	o:write(string.format([[
	{
		info = {
			icon = %q,
			background = %q,
		},
		talents = {
]], tree.info.icon, tree.info.background))
	for _, talent in ipairs(tree.talents) do
		write_talent(o, talent)
	end
	o:write([[
		},
	},
]])
end

local function write_classes ()

	for c in pairs(CLASSES) do
		local info = Talented_GatherDB[build][c]
		if info then
			o = io.open(string.format(OUTPUT.."Talented_Data_%s.lua", c), "w")
			o:write(string.format([[
-- Generated Data, do not modify
if not Talented_Data then return end

Talented_Data.%s = {
]], c))
			for _, tab in ipairs(info) do
				write_tree(o, tab)
			end
			o:write("}\n")
			o:close()
		end
	end
end

local function write_init()
	f = io.open(OUTPUT.."Talented_Data_init.lua", "w")
	f:write(string.format([[
-- Generated Data, do not modify
Talented_Data = {}
]], build))
	f:close()
end

local function write_toc()
	f = io.open(OUTPUT.."Talented_Data.toc", "w")
	f:write(string.format(TOC_HEADER, TOC_VERSION, build))

	for c in pairs(CLASSES) do
		f:write(TOC_ENTRY:format(c))
	end
	for _, l in pairs(LANG_MAP) do
		if not LANG_OVERRIDE[l] then
			f:write(TOC_ENTRY:format(l))
		end
	end
	if next(LANG_OVERRIDE) then
		f:write("# The following language are hand-made additions overriding the generated one\n")
		for l in pairs(LANG_OVERRIDE) do
			f:write(TOC_ENTRY:format(l))
		end
	end
	f:write(TOC_FOOTER)
	f:close()
end

function write_data()
	write_classes()
	write_init()
	write_toc()
end

if not arg or arg[0] == "convert-sv.lua" then
	write_data()
end
